package ac.svn.accesscontrol.ui.window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import ac.notification.event.NotificationEvent;
import ac.notification.manager.NotificationRegistrar;
import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.store.ISVNIdentifier;
import ac.svn.accesscontrol.ui.JMenu;
import ac.svn.accesscontrol.ui.MarginBorder;
import ac.svn.accesscontrol.ui.STextField;
import ac.svn.accesscontrol.ui.StatusLabel;
import ac.svn.accesscontrol.utils.UIUtility;
import ac.svn.accesscontrol.utils.Utility;

public class LocationManagerWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected final JFrame Iam = this;
	private JMenuItem addLocationMI,deleteLocationMI,editLocationMI,closeMI,helpMI;
	private STextField locationNameField,locationPathField;
	private JButton addLocationButton,deleteLocationButton,editLocationButton,doneButton,closeButton, accessRightsBtn;
	private StatusLabel statusLabel;
	private JToolBar toolBar;
	private JPanel centreBasePanel; 

	public LocationManagerWindow()
	{
		super("Location Information Manager");
		initWindow();
		initComponents();
	}

	private void initWindow() 
	{
		setLayout(new BorderLayout(5,10));
		setSize(600, 500);
	}

	private void initComponents() 
	{
		Color WHITE = Color.WHITE;
		Border border = UIUtility.setAllShadowedBorder(null);

		getContentPane().setBackground(WHITE);

		//MenuConfigurations.
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBackground(WHITE);
		menuBar.setBorder(border);

		JMenu menu = new JMenu("Action");

		addLocationMI = new JMenuItem("Add Location");
		addLocationMI.setMnemonic('A');
		editLocationMI = new JMenuItem("Edit Location");
		editLocationMI.setMnemonic('E');
		deleteLocationMI = new JMenuItem("Delete Location");
		deleteLocationMI.setMnemonic('D');

		closeMI = new JMenuItem("Close");
		closeMI.setMnemonic('C');

		helpMI = new JMenuItem("Help");
		closeMI.setMnemonic('C');

		//Add Action menu.
		menu.add(addLocationMI);
		menu.add(editLocationMI);
		menu.add(deleteLocationMI);
		menu.addSeparator();
		menu.add(closeMI);

		menuBar.add(menu);

		//Add Help Menu
		menu = new JMenu("Help");
		menu.add(helpMI);
		menuBar.add(menu);

		//Set menubar on window.
		setJMenuBar(menuBar); 

		// Tool configuration.
		toolBar = new JToolBar(JToolBar.VERTICAL);
		toolBar.setFloatable(false);
		toolBar.setBackground(WHITE);
		toolBar.setBorder(border);

		addLocationButton = new JButton(Utility.getImageIcon(IResources.LOC_ICN_ADD_LOCATIONS));
		addLocationButton.setToolTipText("Add a location");
		addLocationButton.setBackground(WHITE);
		toolBar.add(addLocationButton);

		editLocationButton = new JButton(Utility.getImageIcon(IResources.LOC_ICN_EDIT_LOCATIONS));
		//editLocationButton.setIcon(new CompoundIcon(Axis.Y_AXIS, 2,(Utility.getImageIcon(IResources.LOC_ICN_EDIT_LOCATIONS)), (new TextIcon(editLocationButton,"Edit"))));
		editLocationButton.setToolTipText("Edit selected a location");
		editLocationButton.setBackground(WHITE);
		toolBar.add(editLocationButton);

		deleteLocationButton = new JButton(Utility.getImageIcon(IResources.LOC_ICN_DELETE_LOCATIONS));
		//deleteLocationButton.setIcon(new CompoundIcon(Axis.Y_AXIS, 2,(Utility.getImageIcon(IResources.LOC_ICN_DELETE_LOCATIONS)), (new TextIcon(deleteLocationButton,"Delete"))));
		deleteLocationButton.setToolTipText("Delete selected a location");
		deleteLocationButton.setBackground(WHITE);
		toolBar.add(deleteLocationButton);

		statusLabel 	= new StatusLabel("<HTML>Welcome to <b>Location Information Manager</b></HTML>");
		doneButton 	= new JButton("Done");
		closeButton 	= new JButton("Close");

		JPanel buttonBase 	= new JPanel((new BorderLayout(5,5)),true);
		buttonBase.setBackground(WHITE);
		buttonBase.setBorder(border);

		JPanel buttonPanel 	= new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttonPanel.setBackground(WHITE);

		buttonPanel.add(doneButton);
		buttonPanel.add(closeButton);

		buttonBase.add(statusLabel,BorderLayout.CENTER);
		buttonBase.add(buttonPanel,BorderLayout.EAST);

		centreBasePanel 	= new JPanel((new BorderLayout(5,5)),true);
		centreBasePanel.setBackground(WHITE);
		centreBasePanel.setBorder(border);

		JPanel tempBasePanel 	= new JPanel((new GridLayout(2,1,0,5)),true);
		tempBasePanel.setBackground(WHITE);
		tempBasePanel.setBorder(border);

		JPanel tempPanel 	= new JPanel((new BorderLayout(5,5)),true);
		tempPanel.setBackground(WHITE);
		tempPanel.setBorder(border);

		locationNameField 	= new STextField();
		locationNameField.setBorder(border);
		locationNameField.setEditable(false);

		accessRightsBtn 	= new JButton("Access Rights");

		tempPanel.add((new JLabel(String.format("%20s","Location Name"))),BorderLayout.WEST);
		tempPanel.add(locationNameField,BorderLayout.CENTER);
		tempPanel.add(accessRightsBtn,BorderLayout.EAST);
		tempBasePanel.add(tempPanel);

		tempPanel 		= new JPanel((new BorderLayout(5,5)),true);
		tempPanel.setBackground(WHITE);
		tempPanel.setBorder(border);

		locationPathField 	= new STextField();
		locationPathField.setBorder(border);
		locationPathField.setEditable(false);

		tempPanel.add((new JLabel(String.format("%20s","Location Path"))),BorderLayout.WEST);
		tempPanel.add(locationPathField,BorderLayout.CENTER);
		tempBasePanel.add(tempPanel);

		tempBasePanel.setBorder(new TitledBorder((new MarginBorder()),"Location Details"));

		centreBasePanel.add(tempBasePanel,BorderLayout.NORTH);

		getContentPane().add(toolBar,BorderLayout.WEST);
		getContentPane().add(centreBasePanel,BorderLayout.CENTER);
		getContentPane().add(buttonBase,BorderLayout.SOUTH);	

	}

	public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		LocationManagerWindow locationManagerWindow = new LocationManagerWindow();
		locationManagerWindow.setVisible(true);
	}

	public JToolBar getToolBar() {
		return toolBar;
	}

	public JButton getAddLocationButton() {
		return addLocationButton;
	}

	public JButton getDeleteLocationButton() {
		return deleteLocationButton;
	}

	public JButton getEditLocationButton() {
		return editLocationButton;
	}

	public JButton getDoneButton() {
		return doneButton;
	}

	public JButton getCloseButton() {
		return closeButton;
	}

	public JMenuItem getAddLocationMI() {
		return addLocationMI;
	}

	public JMenuItem getDeleteLocationMI() {
		return deleteLocationMI;
	}

	public JMenuItem getEditLocationMI() {
		return editLocationMI;
	}

	public JMenuItem getCloseMI() {
		return closeMI;
	}

	public JTextField getLocationNameField() {
		return locationNameField;
	}

	public JTextField getLocationPathField() {
		return locationPathField;
	}

	public JMenuItem getHelpMI() {
		return helpMI;
	}

	public void setHelpMI(JMenuItem helpMI) {
		this.helpMI = helpMI;
	}

	public StatusLabel getStatusLabel() {
		return statusLabel;
	}


	public void addLocationListView(Component component)
	{
		if(null != centreBasePanel)
		{
			centreBasePanel.add(component,BorderLayout.CENTER);
			SwingUtilities.invokeLater(new Runnable() {
				@Override
				public void run() {
					SwingUtilities.updateComponentTreeUI(centreBasePanel);
				}
			});
		}
	}

	public void setVisible(boolean b) 
	{
		NotificationEvent event = new NotificationEvent(ISVNIdentifier.N_LOCATION_EDITOR_VISIBILTY_CHANGE);
		event.setPropertyValue(b);
		NotificationRegistrar.dispatchNotification(event);
		super.setVisible(b);
	}

	public JButton getAccessRightsBtn() {
		return accessRightsBtn;
	}    
}
